
package controller;

import jgf.Attribute;
import jgf.beans.ArcBean;
import jgf.beans.NodeBean;
import jgf.exceptions.arc.ArcException;
import jgf.exceptions.arc.DuplicatedArcException;
import jgf.exceptions.arc.UnexistentArcException;
import jgf.exceptions.attribute.AttributeException;
import jgf.exceptions.attribute.BadAttributeListException;
import jgf.exceptions.attribute.DuplicatedAttributeException;
import jgf.exceptions.attribute.UnexistentAttributeException;
import jgf.exceptions.node.DuplicatedNodeException;
import jgf.exceptions.node.UnexistentNodeException;
import model.observer.JGSObservable;

/**
 *
 * @author Marcos Garcia Ortega <a href="mailto:thiamath@gmail.com">&lt;thiamath@gmail.com&gt;</a>
 */
public class GraphController {

    private JGSObservable model;

    /**
     * Creates a GraphController dedicated for the graph passed as argument.
     * @param graph The graph to manage.
     */
    GraphController(JGSObservable graph) {
		this.model = graph;
    }

	/**
	 * Sets the name of the controlled model.
	 * @param name The new name for the graph.
	 */
	public void setName(String name) {
		model.setName(name);
	}

	/**
	 * Inserts a node into the controlled model.
	 * @param node The bean of the new node to insert.
	 * @throws BadAttributeListException If the attribute list of the given
	 *	NodeBean is not correct, this exception will raise.
	 */
	public void insertNode(NodeBean node)
			throws BadAttributeListException {
		model.insertNode(node);
	}

	/**
	 * Deletes a node from the controlled model.
	 * @param node The bean of the node to delete.
	 * @throws UnexistentNodeException If the information of the bean points to
	 *		a node that is not in the model, this exception will raise.
	 */
	public void deleteNode(NodeBean node)
			throws UnexistentNodeException {
		model.deleteNode(node);
	}

	/**
	 * Updates the information of a node from the controlled model.
	 * @param node The new information for the node in bean terms.
	 * @throws UnexistentNodeException If the information of the bean points to
	 *		a node that is not in the model, this exception will raise.
	 * @throws BadAttributeListException If the attribute list of the given
	 *		NodeBean is not correct, this exception will raise.
	 */
	public void updateNode(NodeBean node)
			throws UnexistentNodeException,
			BadAttributeListException {
		model.updateNode(node);
	}

	/**
	 * Sets the given node to be source in the model.
	 * @param node The node that will become source.
	 * @throws DuplicatedNodeException If the node is already source,
	 *		this exception will raise.
	 * @throws UnexistentNodeException If the node to insert into the sources
	 *		do not exist, then this exception will raise.
	 */
	public void setSource(NodeBean node)
			throws DuplicatedNodeException,
			UnexistentNodeException {
		model.setSource(node);
	}

	/**
	 * Usets the state of source to the node in the model.
	 * @param node The node that will be fired from the sources list.
	 * @throws UnexistentNodeException If the node to remove from the sources
	 *		is not there, then this exception will raise.
	 */
	public void unsetSource(NodeBean node)
			throws UnexistentNodeException {
		model.unsetSource(node);
	}

	/**
	 * Sets the given node to be sink in the model.
	 * @param node The node that will become sink.
	 * @throws DuplicatedNodeException If the node is already sink,
	 *		this exception will raise.
	 * @throws UnexistentNodeException If the node to insert into the sinks
	 *		do not exist, then this exception will raise.
	 */
	public void setSink(NodeBean node)
			throws DuplicatedNodeException,
			UnexistentNodeException {
		model.setSink(node);
	}

	/**
	 * Usets the state of sink to the node in the model.
	 * @param node The node that will be fired from the sinks list.
	 * @throws UnexistentNodeException If the node to remove from the sinks
	 *		is not there, then this exception will raise.
	 */
	public void unsetSink(NodeBean node)
			throws UnexistentNodeException {
		model.unsetSink(node);
	}

	/**
	 * Inserts a new arc into the model and transmits the change to the observers.
	 * @param arc The new arc to insert. Must be filled with the node IDs.
	 * @throws DuplicatedArcException If you try to insert an arc between
	 *		two nodes that have already an arc and the graph is not multiple,
	 *		this exception will raise.
	 * @throws UnexistentNodeException If you try to insert an arc that have
	 *		the same node as 'from' and 'to' and the graph is not a pseudograph,
	 *		this exception will raise.
	 * @throws DuplicatedNodeException If one of the given nodes to define
	 *		the arc do not exist on the graph, this exception will raise.
	 * @throws ArcException If there is an generic problem, as null pointers
	 *		etc. this exception will raise.
	 */
	public void insertArc(ArcBean arc)
			throws DuplicatedArcException,
			UnexistentNodeException,
			DuplicatedNodeException,
			ArcException {
		model.insertArc(arc);
	}

	/**
	 * Removes the arc from the model and transmits the change to the observers.
	 * @param arc The arc to remove from the model.
	 * @throws UnexistentArcException If the given ID number is not mapped
	 *		in the arc tree, this exception will raise.
	 */
	public void deleteArc(ArcBean arc)
			throws UnexistentArcException {
		model.deleteArc(arc);
	}

	/**
	 * Updates the information of the arc and transmits the change
	 * to the observers.
	 * @param arc The new information of the arc.
	 * @throws UnexistentArcException If the given ID number do not correspond
	 *		with any arc in the graph, this exception will raise.
	 * @throws BadAttributeListException If any of the given attributes
	 *		do not match with the attributes assigned to arcs on the graph,
	 *		this exception will raise.
	 */
	public void updateArc(ArcBean arc)
			throws UnexistentArcException,
			BadAttributeListException {
		model.updateArc(arc);
	}

	/**
	 * Adds a new default attribute for the nodes and transmits the change
	 * to the observers.
	 * @param attribute The new attribute for the nodes.
	 * @throws DuplicatedAttributeException If the selected name for
	 *		the attribute is already present on the model,
	 *		this exception will raise.
	 */
	public void addDefaultNodeAttribute(Attribute attribute)
			throws DuplicatedAttributeException {
		model.addDefaultNodeAttribute(attribute);
	}

	/**
	 * Removes a default attribute for nodes from the model and transmits
	 * the change to the observers.
	 * @param attribute The attribute to delete.
	 * @throws UnexistentAttributeException If the attribute does not exist
	 *		for nodes on this graph, this exception will raise.
	 */
	public void removeDefaultNodeAttribute(Attribute attribute)
			throws UnexistentAttributeException {
		model.removeDefaultNodeAttribute(attribute);
	}

	/**
	 * Changes the information of a default attribute on the model and transmits
	 * the change to the observers.
	 * @param previousName The name of the attribute to change.
	 * @param newValue The new attribute that will substitute the previous one.
	 * @throws AttributeException If any of the given attributes is null,
	 *		this exception will raise.
	 * @throws DuplicatedAttributeException If the new name already exists on
	 *		the list of default attributes for nodes, this exception will raise.
	 * @throws UnexistentAttributeException If the previous name does not exist
	 *		on the list of default attributes for nodes,
	 *		this exception will raise.
	 */
	public void updateDefaultNodeAttribute(String previousName, Attribute newValue)
			throws AttributeException,
			DuplicatedAttributeException,
			UnexistentAttributeException {
		model.updateDefaultNodeAttribute(previousName, newValue);
	}

	/**
	 * Adds a new default attribute for the arcs and transmits the change to
	 * the observers.
	 * @param attribute The new attribute to add.
	 * @throws DuplicatedAttributeException If the selected name for the
	 *		attribute is already present on the model,
	 *		this exception will raise.
	 */
	public void addDefaultArcAttribute(Attribute attribute)
			throws DuplicatedAttributeException {
		model.addDefaultArcAttribute(attribute);
	}

	/**
	 * Removes a default attribute for arcs from the model and transmits
	 * the change to the observers.
	 * @param attribute The attribute to delete.
	 * @throws UnexistentAttributeException If the attribute does not exist
	 *		for nodes on this graph, this exception will raise.
	 */
	public void removeDefaultArcAttribute(Attribute attribute)
			throws UnexistentAttributeException {
		model.removeDefaultArcAttribute(attribute);
	}

	/**
	 * Changes the information of a default attribute on the model
	 * and transmits the change to the observers.
	 * @param previousName The name of the attribute to change.
	 * @param newValue The ne attribute that will substitute the previous one.
	 * @throws AttributeException If any of the given attributes is null,
	 *		this exception will raise.
	 * @throws DuplicatedAttributeException If the new name already exists on
	 *		the list of default attributes for nodes, this exception will raise.
	 * @throws UnexistentAttributeException If the previous name does not exist
	 *		on the list of default attributes for nodes,
	 *		this exception will raise.
	 */
	public void updateDefaultArcAttribute(String previousName, Attribute newValue)
			throws AttributeException,
			DuplicatedAttributeException,
			UnexistentAttributeException {
		model.updateDefaultArcAttribute(previousName, newValue);
	}

}
